/**
 * 
 */
package wpmp.security.mgr.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.stereotype.Component;

import wpmp.security.mgr.base.BaseDao;
import wpmp.security.mgr.entity.Operation;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 2:41:35 PM Mar 18, 2013
 */
@Component
public class OperationDao extends BaseDao<Operation, Long> {

	private static final String SQL_UPDATE_ORDER_NUM = "UPDATE sys_operation SET order_num=? WHERE id=?";

	private static final String SQL_DELETE_RESOURCE_OPERATION_RELA = "DELETE FROM sys_resource_operation_rela WHERE operation_id=?";

	private static final String SQL_DELETE_ROLE_RESOURCE_OPERATION_RELA = "DELETE FROM sys_role_resource_operation_rela WHERE operation_id=?";

	/**
	 * 删除角色、资源、操作类型之间的关系
	 * 
	 * @param operationId
	 * @return
	 */
	public Integer deleteRoleResourceOperationRela(final Long operationId) {
		return getJdbcTemplate().execute(SQL_DELETE_ROLE_RESOURCE_OPERATION_RELA,
				new PreparedStatementCallback<Integer>() {
					@Override
					public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException,
							DataAccessException {
						ps.setLong(1, operationId);
						return ps.executeUpdate();
					}
				});
	}

	/**
	 * 删除角色、资源、操作类型之间的关系
	 * 
	 * @param operationIds
	 * @return
	 */
	public int[] deleteRoleResourceOperationRela(final Long[] operationIds) {
		return getJdbcTemplate().batchUpdate(SQL_DELETE_ROLE_RESOURCE_OPERATION_RELA,
				new BatchPreparedStatementSetter() {

					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						ps.setLong(1, operationIds[i]);
					}

					@Override
					public int getBatchSize() {
						return operationIds.length;
					}
				});
	}

	/**
	 * 删除资源、操作类型之间的关系
	 * 
	 * @param operationId
	 * @return
	 */
	public Integer deleteResourceOperationRela(final Long operationId) {
		return getJdbcTemplate().execute(SQL_DELETE_RESOURCE_OPERATION_RELA,
				new PreparedStatementCallback<Integer>() {
					@Override
					public Integer doInPreparedStatement(PreparedStatement ps) throws SQLException,
							DataAccessException {
						ps.setLong(1, operationId);
						return ps.executeUpdate();
					}
				});
	}

	/**
	 * 删除资源、操作类型之间的关系
	 * 
	 * @param operationIds
	 * @return
	 */
	public int[] deleteResourceOperationRela(final Long[] operationIds) {
		return getJdbcTemplate().batchUpdate(SQL_DELETE_RESOURCE_OPERATION_RELA,
				new BatchPreparedStatementSetter() {

					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						ps.setLong(1, operationIds[i]);
					}

					@Override
					public int getBatchSize() {
						return operationIds.length;
					}
				});
	}

	/**
	 * 更新排序号
	 * 
	 * @param ids
	 * @param orderNums
	 * @return
	 */
	public int[] updateOrderNum(final Long[] ids, final Integer[] orderNums) {
		return getJdbcTemplate().batchUpdate(SQL_UPDATE_ORDER_NUM,
				new BatchPreparedStatementSetter() {

					@Override
					public void setValues(PreparedStatement ps, int i) throws SQLException {
						ps.setInt(1, orderNums[i]);
						ps.setLong(2, ids[i]);
					}

					@Override
					public int getBatchSize() {
						return ids.length;
					}
				});
	}
}
